package org.dbstar.scheduler.beans;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.dbstar.scheduler.identifier.Identifier;
import org.dbstar.scheduler.identifier.IdentifierType;


/**
 * Job的定义类
 * 
 * @author 代波
 * @since 1.0.0
 * @version 1.0.0
 * @date 2012-3-21
 */
public abstract class JobConf implements Serializable, Cloneable, Comparable<JobConf> {
	private static final long serialVersionUID = 5524919502385857631L;

	private Identifier identifier;// 标识
	private int catalog;// 目录编号
	private String name;// Job名称
	private String description;// Job描述
	private String resource;// Job所使用服务器资源
	private List<ParameterDefine> parameters;// Job参数定义
	private int maxRetry;// 最大失败重试次数
	private long createTime = System.currentTimeMillis();// 创建时间
	private Long modifyTime;// 最后修改时间
	private String owner;// 所有者，默认为创建人
	private String modifier;// 最后修改人
	private int averageRunTime = -1;// 平均执行耗时

	public final Identifier getIdentifier() {
		return identifier;
	}

	public final void setIdentifier(Identifier identifier) {
		if (identifier == null) {
			throw new IllegalArgumentException("Identifier must not null.");
		}
		if (identifier.getIdentifierType() != IdentifierType.conf) {
			throw new IllegalArgumentException("Identifier not a Job Configuration identifier: "
					+ identifier.getStringIdentifier());
		}
		this.identifier = identifier;
	}

	public final int getCatalog() {
		return catalog;
	}

	public final void setCatalog(int catalog) {
		this.catalog = catalog;
	}

	public final String getName() {
		return name;
	}

	public final void setName(String name) {
		this.name = name;
	}

	public final String getDescription() {
		return description;
	}

	public final void setDescription(String description) {
		this.description = description;
	}

	public String getResource() {
		return resource;
	}

	public void setResource(String resource) {
		this.resource = StringUtils.isBlank(resource) ? null : resource;
	}

	public final List<ParameterDefine> getParameters() {
		return parameters;
	}

	public final void setParameters(List<ParameterDefine> parameters) {
		this.parameters = parameters == null || parameters.isEmpty() ? null : parameters;
	}

	public final int getParameterSize() {
		return parameters == null ? 0 : parameters.size();
	}

	public final int getMaxRetry() {
		return maxRetry;
	}

	public final void setMaxRetry(int maxRetry) {
		this.maxRetry = maxRetry;
	}

	public final long getCreateTime() {
		return createTime;
	}

	public final void setCreateTime(long createTime) {
		this.createTime = createTime;
	}

	public final Long getModifyTime() {
		return modifyTime;
	}

	public final void setModifyTime(Long modifyTime) {
		this.modifyTime = modifyTime;
	}

	public String getOwner() {
		return owner;
	}

	public void setOwner(String owner) {
		this.owner = owner;
	}

	public String getModifier() {
		return modifier;
	}

	public void setModifier(String modifier) {
		this.modifier = modifier;
	}

	public int getAverageRunTime() {
		return averageRunTime;
	}

	public void setAverageRunTime(int averageRunTime) {
		this.averageRunTime = averageRunTime;
	}

	@Override
	public JobConf clone() throws CloneNotSupportedException {
		JobConf conf = (JobConf) super.clone();
		if (parameters != null) {
			conf.parameters = new ArrayList<ParameterDefine>(parameters);
		}
		return conf;
	}

	@Override
	public final int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((identifier == null) ? 0 : identifier.hashCode());
		return result;
	}

	@Override
	public final boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (!JobConf.class.isInstance(obj))
			return false;
		JobConf other = (JobConf) obj;
		if (identifier == null) {
			if (other.identifier != null)
				return false;
		} else if (!identifier.equals(other.identifier))
			return false;
		return true;
	}

	@Override
	public int compareTo(JobConf o) {
		if (catalog < o.catalog) {
			return -1;
		} else if (catalog > o.catalog) {
			return 1;
		} else {
			return identifier.compareTo(o.identifier);
		}
	}

	/**
	 * 获得JobConf的类型
	 * 
	 * @return JobConf的类型
	 */
	public abstract String getType();

	/**
	 * 获得JobConf的内容
	 * 
	 * @return JobConf的内容
	 */
	public abstract String getContent();

	/**
	 * 设置JobConf的内容
	 * 
	 * @param content 内容字符串
	 */
	public abstract void setContent(String content);
}
